<template>
  <!-- 添加或修改盘点计划对话框 -->
  <el-dialog :title="title" v-if="open" v-model="open" width="550px" append-to-body>

    <el-form class="common_upload" ref="form" :model="form" :rules="rules" label-width="130px">

      <el-form-item label="素材名称:" prop="materialName">
        <el-input v-model="form.materialName"></el-input>
      </el-form-item>
      <el-form-item label="选择分组:" prop="groupId">
        <el-select style="width: 100%;" v-model="form.groupId" value-key="" placeholder="" clearable filterable>
          <el-option v-for="item in fodders" :key="item.id" :label="item.groupName" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择图片:" prop="materialUrl" class="goods_imgs">
        <el-upload class="avatar-uploader el-upload" :headers="headers"
          :on-success="(res, file) => handleUploadSuccess(res, file, 'materialUrl')" :show-file-list="false"
          :action="uploadUrl">
          <img ref="img" v-if="form.materialUrl" :src="form.materialUrl" class="avatar">
          <div v-else class="upload-text">点击上传图片</div>
          <div class="btn" v-if="form.materialUrl">
            <el-button type="text" icon="el-icon-delete" @click.prevent="deleteImg('materialUrl')"></el-button>
          </div>
        </el-upload>
        <el-input v-model="form.materialUrl" v-show="false"></el-input>
      </el-form-item>

      <el-form-item label="描述:">
        <el-input type="textarea" :rows="4" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import modelMixin from '@/mixins/modelMixin'
import common from '@/api/goods/common/index';
import { listMaterialGroup } from '@/api/goods/group/index'
import { globalHeaders } from '@/utils/request';
import useUserStore from '@/store/modules/user';
const userStore = useUserStore();
export default {
  name: "uploadForm",
  mixins: [modelMixin],
  props: {
    classifyList: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      uploadUrl: import.meta.env.VITE_APP_BASE_API + '/resource/oss/upload', // 上传的图片服务器地址
      headers: globalHeaders(),
      funcObj: common,//方法对象
      open: false,
      title: "上传图片",
      form: {
        // 素材类型：0=图片1=视频2=音频3=其他
        materialType: 0,
      },
      fodders: [],
      rules: {
        groupId: [
          { required: true, message: "请选择分组", trigger: "blur" }
        ],
        materialName: [
          { required: true, message: "请输入素材名称", trigger: "blur" }
        ],
        materialUrl: [
          { required: true, message: "请上传图片", trigger: "blur" }
        ],
      },
      // classifyList: [],
    }
  },
  created() {
    // this.getClassifyAllList()
  },
  methods: {
    add() {
      this.open = true;
      this.getGroupList()
      this.form = {
        materialType: 0,
        userId: userStore.userId
      }
      this.title = "上传图片"

    },
    edit(row) {
      this.open = true;
      this.getGroupList()
      this.form = JSON.parse(JSON.stringify(row))
      this.title = "修改图片"
    },
    classifyForm() {
      this.form.materialType = 0
    },

    getGroupList() {
      listMaterialGroup().then(res => {
        if (res.code == 200) {
          this.fodders = res.rows
        } else {
          this.$modal.msgError(res.msg)
        }
      }).catch(err => {
        console.log('err :>> ', err);
      })
    },


    // 上传成功
    async handleUploadSuccess(res, file, key) {
      // 如果上传成功
      if (res.code == 200) {
        this.form[key] = res.data.url;
        this.form.materialSize = file.size
      } else {
        console.log(res);
      }
    },
  }

}
</script>
<style type="text/css" lang="scss" scoped>
.avatar-uploader,
.el-upload {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: inline-block;
  margin-right: 10px;
}

.avatar-uploader:hover,
.el-upload:hover {
  border-color: #409eff;
}

.imgs {
  display: inline-block;
}

.avatar-uploader__img {
  border: 1px solid #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 178px !important;
  height: 178px;
  display: inline-block;
  margin-right: 10px;

  img {
    width: 178px !important;
    height: 178px;
    object-fit: fill;
  }
}

.btn {
  display: none;
  position: absolute;
  top: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  width: 178px !important;
  height: 178px;
  border-radius: 0 6px 0 6px;
  text-align: center;
  line-height: 178px;
}

.avatar-uploader:hover {

  // 鼠标悬浮显示按钮
  .btn {
    display: block;
  }

}

.upload-text {
  font-size: 14px;
  color: #8c939d;
  line-height: 178px;
  text-align: center;
  margin: 0 auto;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style lang="scss">
.el-form-item__content {
  width: auto !important;
}

.common_upload .el-upload {
  display: block !important;
}
</style>